﻿#include "musicwindow.h"
#include "ui_musicwindow.h"

musicWindow::musicWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::musicWindow)
{
    ui->setupUi(this);
    ui->label_2->setPixmap(QPixmap(":/4.png"));
    ui->label_2->setScaledContents(true);
    net=new QNetworkAccessManager(this);
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    ui->lineEdit->setPlaceholderText("请输入你要搜索的歌手或者歌曲");
    QStandardItemModel *mod1=new QStandardItemModel(ui->tableView);
    QVector<QString> songs={"光年之外","泡沫","喜欢你","倒数",
                           "忘情水","冰雨","爱你一万年","一起走过的日子",
                           "青花瓷","告白气球","七里香","稻香",
                           "情人","YOUNG","Wait Wait Wait","没有意外"
                           };
    int num=0;
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            mod1->setItem(i,j,new QStandardItem(songs[num++]));
        }
    }
    mod1->setVerticalHeaderLabels({"邓紫棋","刘德华","周杰伦","蔡徐坤"});
    mod1->setHorizontalHeaderLabels({"代表作1","代表作2","代表作3","代表作4"});
    ui->label->setType(2);
    connect(ui->label,&myLabel::find,this,&musicWindow::on_label_find);
    ui->tableView->setModel(mod1);
    connect(net,&QNetworkAccessManager::finished,this,&musicWindow::on_net_finish);
}
void musicWindow::backWidget(QWidget *parent)
{
    back=parent;
}

musicWindow::~musicWindow()
{
    delete ui;
}

void musicWindow::on_pushButton_2_clicked()
{
    close();
    back->show();
}

void musicWindow::on_label_find()
{
    QString data=ui->lineEdit->text();
    QString address = "http://music.163.com/api/search/get/web?csrf_token=hlpretag=&hlposttag=&s=" ;
    address+=data;
    address += "&type=1&offset=0&total=true&limit=10";
    net->get(QNetworkRequest(QUrl(address)));
}

void musicWindow::on_net_finish(QNetworkReply *reply)
{
    int status=reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    if(status!=200)
    {
        qDebug("网络连接错误");
        return;
    }
    else
    {
        qDebug()<<reply->readAll();
    }
}

void musicWindow::on_pushButton_3_clicked()
{
    close();
}
